package cn.jiguang.jgadpubadapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import cn.jiguang.jgssp.ADJgSdk;
import cn.jiguang.jgssp.config.ADJgInitConfig;
import cn.jiguang.jgssp.config.CustomDeviceInfoController;
import cn.jiguang.jgssp.listener.ADJgInitListener;
import xyz.adscope.amps.AMPSSDK;
import xyz.adscope.amps.common.AMPSError;
import xyz.adscope.amps.common.AMPSErrorCode;
import xyz.adscope.amps.init.AMPSInitAdapterConfig;
import xyz.adscope.amps.init.inter.AMPSChannelInitMediation;
import xyz.adscope.amps.init.inter.IAMPSChannelInitCallBack;

public class JgPubAdInit extends AMPSChannelInitMediation {

    private static JgPubAdInit instance;
    private boolean isInit = false;

    public synchronized static JgPubAdInit getInstance() {
        if (instance == null) {
            synchronized (JgPubAdInit.class) {
                instance = new JgPubAdInit();
            }
        }
        return instance;
    }

    private static final String TAG = "JgPubAdInit";

    @Override
    public void initSDK(AMPSInitAdapterConfig params,
                        IAMPSChannelInitCallBack callBack) {
        if (ADJgSdk.getInstance().isInit() || isInit) {
            initSDKSuccess(callBack);
            return;
        }

        Context context = AMPSSDK.getContext();
        if (context == null || params == null || TextUtils.isEmpty(params.getAppId())) {
            initSDKFail(callBack,
                    new AMPSError(AMPSErrorCode.ChannelErrorEnum.CHANNEL_ERROR_INIT_ERROR.getErrorCode(), AMPSErrorCode.ChannelErrorEnum.CHANNEL_ERROR_INIT_ERROR.getErrorMsg()));
            return;
        }

        ADJgSdk.getInstance().init(context, new ADJgInitConfig.Builder()
                .appId(params.getAppId())
                .isCanUseWifiState(JGBZInitManager.getInstance().isCanUseWifiState())
                .debug(JGBZInitManager.getInstance().isDebug())
                // 是否可获取定位数据
                .isCanUseLocation(JGBZInitManager.getInstance().isCanUseLocation())
                // 是否可获取设备信息
                .isCanUsePhoneState(JGBZInitManager.getInstance().isCanUsePhoneState())
                .setCustomDeviceInfoController(new CustomDeviceInfoController() {
                    @Override
                    public String getOaid() {
                        return JGBZInitManager.getInstance().getOaid();
                    }

                    @Override
                    public String getVaid() {
                        return JGBZInitManager.getInstance().getVaid();
                    }

                    @Override
                    public String getAndroidId() {
                        return JGBZInitManager.getInstance().geAndroidId();
                    }

                    @Override
                    public String getMacAddress() {
                        return JGBZInitManager.getInstance().getMac();
                    }

                    @Override
                    public String getImei() {
                        return JGBZInitManager.getInstance().getImei();
                    }
                })
                .build(), new ADJgInitListener() {
            @Override
            public void onSuccess() {
                isInit = true;
                LogUtil.d(TAG, "jiguang adapter initSDK: success");

                initSDKSuccess(callBack);
            }

            @Override
            public void onFailed(String s) {
                LogUtil.d(TAG, "jiguang adapter initSDK: onFailed");
                initSDKFail(callBack, new AMPSError("", s));
            }
        });
    }

}
